package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.DispatchBean;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;


public class AddDispatchListActivity extends BaseActivity {



    @InjectView(R.id.Toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.btn_push_data)
    Button mBtnPushData;
    @InjectView(R.id.et_dispatch_carnumber)
    EditText mEtDispatchCarnumber;
    @InjectView(R.id.ti_dispatch_carnumber)
    TextInputLayout mTiDispatchCarnumber;
    @InjectView(R.id.et_dispatch_parkid)
    EditText mEtDispatchParkid;
    @InjectView(R.id.ti_dispatch_parkid)
    TextInputLayout mTiDispatchParkid;
    @InjectView(R.id.tv_remarks)
    EditText mTvRemarks;
    @InjectView(R.id.ll_dispatch_list)
    LinearLayout mLlDispatchList;
    private EditText mEditTextCarNumber;
    private EditText mEditTextParkId;
    SweetAlertDialog sweetAlertDialog;
    private String mId;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_dispatch_list);
        ButterKnife.inject(this);
        //注册EvenBus
        EventBus.getDefault().register(this);
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        mToolbar.setTitle("赳猎人");
        mToolbar.setSubtitle("填写调度单");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.backbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mEditTextCarNumber = mTiDispatchCarnumber.getEditText();

        mEditTextCarNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    mBtnPushData.setEnabled(true);
                } else {
                    mBtnPushData.setEnabled(false);
                }
            }
        });
        mEditTextParkId = mTiDispatchParkid.getEditText();
        mEditTextParkId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    mBtnPushData.setEnabled(true);
                } else {
                    mBtnPushData.setEnabled(false);
                }
            }
        });
        mEditTextParkId.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Intent intent = new Intent(AddDispatchListActivity.this, PointMessage.class);
                        intent.putExtra("adddispatchlist", "1");
                        startActivity(intent);
                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                    case MotionEvent.ACTION_UP:

                        break;
                    default:
                        break;
                }
                return false;
            }
        });

    }


    @OnClick({R.id.btn_push_data, R.id.et_dispatch_carnumber, R.id.et_dispatch_parkid})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_push_data:
                String carNumber = mEditTextCarNumber.getText().toString().trim();
                String remarks = mTvRemarks.getText().toString().trim();
                if (!TextUtils.isEmpty(carNumber) && !TextUtils.isEmpty(mName)) {
                    upLoadData(carNumber, remarks);
                    break;
                }
                showToast("请将数据输入完整");
            case R.id.et_dispatch_carnumber:
                break;
            default:
                break;
        }
    }

    //上传数据
    private void upLoadData(String carNumber, String remarks) {
        RequestBody formBody = new FormBody.Builder()
                .add("action", "ADDDISPATCHLIST")
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(AddDispatchListActivity.this, "adminId", "")))
                .add("schedulingQuantity", carNumber)
                .add("locatingStopId", mId)
                .add("remarks", remarks)
                .build();
        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationperson?")
                .post(formBody)
                .build();
        postRequest(request,"ADDDISPATCHLIST");
    }

    @Subscribe
    public void onEventMainThread(DispatchBean event) {
        mId = event.getId();
        mName = event.getName();
        mEditTextParkId.setText(mName);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //反注册
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
                        ResponseBody body = response.body();
                        if (body != null) {
                            try {
                                String  string = body.string();
                                final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                                if (mse != null) {
                                    if (mse.getCode() == 1) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                sweetAlertDialog.dismiss();
                                                showToast( mse.getMessage() + "");
                                                finish();
                                            }
                                        });

                                    } else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                sweetAlertDialog.dismiss();
                                                showToast(mse.getMessage() + "");
                                                finish();
                                            }
                                        });
                                    }
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            sweetAlertDialog.dismiss();
                                            showToast("数据异常请重试");
                                            finish();
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    sweetAlertDialog.dismiss();
                                    showToast("数据异常请重试");
                                    finish();
                                }
                            });
                        }


    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sweetAlertDialog.dismiss();
                showToast("网络异常请重试");
                finish();
            }
        });
    }
}
